************************************
***** 		Elite Cues 		   *****
*District-Leve Analysis*************
************************************

* December 2014

*** DISTRICT LEVEL ANALYSIS ***

*This do file creates the district-level models and a figure of emphasis by position.  Coefficients are written to separate files and 
*used in the R file called "plots_districtmodles.R"

clear
use 2010_district_expanded_for_R.dta


postfile temp_file brc serc bcp secp bcp_p secp_p bdistideo sedistideo bimmig seimmig bimmig_p seimmig_p bunemp seunemp bhisp sehisp bhisp_p sehisp_p bhd0010 sehd0010 bborder seborder bborderp seborderp bnewdest1 senewdest1   using districtmodel.dta, replace 
foreach y of varlist candidate_priority  { 
  di _n as input ". ologit `y', by(controls)"  
    reg `y'  repub_candidate candidate_position pos_p distideo  immig_dist1 immig_p unemp hisppct_10 hisp_p hd0010  border border_p newdest1 , cluster(district)

    scalar brc=_b[repub_candidate]
	scalar serc=_se[repub_candidate]
	scalar bcp=_b[candidate_position]
	scalar secp=_se[candidate_position]
	scalar bcp_p=_b[pos_p]
	scalar secp_p=_se[pos_p]
    scalar bdistideo=_b[distideo]
	scalar  sedistideo=_se[distideo]
    scalar bimmig = _b[immig_dist1]
	scalar seimmig=_se[immig_dist1]
	scalar bimmig_p=_b[immig_p]
	scalar seimmig_p=_se[immig_p]
	scalar bunemp=_b[unemp]
	scalar seunemp=_se[unemp]
	scalar bhisp=_b[hisppct_10]
	scalar sehisp=_se[hisppct_10]
	scalar bhisp_p=_b[hisp_p]
	scalar sehisp_p=_se[hisp_p]
	scalar bhd0010=_b[hd0010]
	scalar sehd0010=_se[hd0010]
	scalar bborder=_b[border]
	scalar seborder=_se[border]
	scalar bborderp=_b[border_p]
	scalar seborderp=_se[border_p]
	scalar bnewdest1=_b[newdest1]
	scalar senewdest1=_se[newdest1]
	
	
   post temp_file (brc) (serc) (bcp) (secp) (bcp_p) (secp_p) (bdistideo) (sedistideo) (bimmig) (seimmig) (bimmig_p) (seimmig_p) (bunemp) (seunemp) (bhisp) (sehisp) (bhisp_p) (sehisp_p) (bhd0010) (sehd0010) (bborder) (seborder) (bborderp) (seborderp) (bnewdest1) (senewdest1)  
   }
postclose temp_file



****************
clear

use districtmodel.dta

expand=13

gen obs=_n

gen beta=brc if obs==1
    replace beta=bcp if obs==2
    replace beta=bcp_p if obs==3
    replace beta=bdistideo if obs==4
    replace beta=bimmig if obs==5
    replace beta=bimmig_p if obs==6
    replace beta=bunemp if obs==7
    replace beta=bhisp if obs==8
    replace beta=bhisp_p if obs==9
	replace beta=bhd0010 if obs==10
    replace beta=bborder if obs==11
	replace beta=bborderp if obs==12
    replace beta=bnewdest1 if obs==13
	
    

gen sebeta=serc if obs==1
    replace sebeta=secp if obs==2
    replace sebeta=secp_p if obs==3
    replace sebeta=sedistideo if obs==4
    replace sebeta=seimmig if obs==5
    replace sebeta=seimmig_p if obs==6
    replace sebeta=seunemp if obs==7
    replace sebeta=sehisp if obs==8
    replace sebeta=sehisp_p if obs==9
	replace sebeta=sehd0010 if obs==10
    replace sebeta=seborder if obs==11
	replace sebeta=seborderp if obs==12
    replace sebeta=senewdest1 if obs==13

      
generate str var3 = "Party" in 1
 replace var3 = "Position" in 2
 replace var3 = "Position x Party" in 3
 replace var3 = "District Ideology" in 4
 replace var3 = "Immigration Opinion" in 5
 replace var3 = "Imm. Op. x Party" in 6
 replace var3 = "District Unemp." in 7 
 replace var3 = "District Hispanic" in 8
 replace var3 = "Dist. Hisp. x Party" in 9
 replace var3 = "Change in Dist. Hisp." in 10
 replace var3 = "Traditional Receiving" in 11
 replace var3 = "Trad. x Party" in 12
 replace var3 = "New Destination" in 13
 
   
  rename var3 label
  
drop bhd0010 sehd0010 bborderp seborderp brc serc bcp secp bcp_p secp_p bdistideo sedistideo bimmig seimmig bimmig_p seimmig_p bunemp seunemp bhisp sehisp bhisp_p sehisp_p bborder seborder bnewdest1 senewdest1 


saveold districtmodel.dta, replace 

clear 

 
use 2010_district_expanded_for_R.dta
 
 
 
 
reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*1. Interpreting the Model: Position and Priority --Republicans*

 margins, at(candidate_position=(4(.5)7) repub_candidate=(1))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=1
saveold posplot_GOP, replace

clear



use 2010_district_expanded_for_R.dta, clear

*REDUCED POOLED MODEL*
reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*2. Interpreting the Model: Position and Priority --Democrats*

 margins, at(candidate_position=(1(.5)4) repub_candidate=(0))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=0
saveold posplot_DEM, replace

clear



use 2010_district_expanded_for_R.dta, clear


reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*3 Immigration Policy, New Dest=1, and Republican
 margins, at(immig_dist1=(0(.1)1) newdest1=(1) repub_candidate=(1))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=1
saveold immRNDplot_GOP, replace

clear



use 2010_district_expanded_for_R.dta, clear

*REDUCED POOLED MODEL*
reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*4. Interpreting the Model: New Destination, Immigration Opinion and Priority --Republicans in NON-ND States

margins, at(immig_dist1=(0(.1)1) newdest1=(0) repub_candidate=(1))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=0
saveold immRnNDplot_GOP, replace

clear



use 2010_district_expanded_for_R.dta, clear

reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*5. Interpreting the Model: New Destination, Immigration Opinion and Priority --Democrat in ND States*

 margins, at(immig_dist1=(0(.1)1) newdest1=(1) repub_candidate=(0))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=1
saveold immDNDplot_DEM, replace

clear



use 2010_district_expanded_for_R.dta, clear

*REDUCED POOLED MODEL*
reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*6. Interpreting the Model: New Destination, Immigration Opinion and Priority --Democrat in NON-ND States

margins, at(immig_dist1=(0(.1)1) newdest1=(0) repub_candidate=(0))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=0
saveold immDnNDplot_DEM, replace

clear



use 2010_district_expanded_for_R.dta, clear

reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*7. Interpreting the Model: District Hispanic/Republican*

 margins, at(hisppct_10=(0(.1).8) repub_candidate=(1))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=1
saveold hispplot_GOP, replace

clear



use 2010_district_expanded_for_R.dta, clear

*REDUCED POOLED MODEL*
reg candidate_priority i.repub_candidate##c.candidate_position c.distideo c.immig_dist1##i.repub_candidate c.unemp i.border##i.repub_candidate i.newdest1 i.repub_candidate##c.hisppct_10 c.hd0010, cluster(district) 


*8. Interpreting the Model: District Hispanic/Dem*

 margins, at(hisppct_10=(0(.1).8) repub_candidate=(0))
   marginsplot
 
matrix pr=r(b)' 
matrix var=vecdiag(r(V))'
drop * 
svmat pr  
svmat var
g se=sqrt(var)
gen repub=0
saveold hispplot_DEM, replace

clear


